package com.wxg.shiro;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.config.IniSecurityManagerFactory;
import org.apache.shiro.mgt.SecurityManager;
import org.apache.shiro.subject.Subject;
import org.apache.shiro.util.Factory;
import org.junit.Assert;
import org.junit.Test;

public class LoginLogoutTest {

	@Test
	public void testHelloWorld(){
		// 1. 获取`SecurityManager`工厂, 此处使用INI配置文件初始化`SecurityManager`
		Factory<SecurityManager> factory = null;
		factory = new IniSecurityManagerFactory("classpath:shiro.ini");
		// 2. 得到`SecurityManager`实例并绑定给`SecurityUtils`
		SecurityManager manager = factory.getInstance();
		SecurityUtils.setSecurityManager(manager);
		// 3. 得到Subject及创建用户名/密码身份验证Token(即用户身份/凭证)
		Subject subject = SecurityUtils.getSubject();
		UsernamePasswordToken token = new UsernamePasswordToken("zhang", "123");
		try {
			subject.login(token);
		} catch (AuthenticationException e) {
			e.printStackTrace();
		}
		Assert.assertEquals(true, subject.isAuthenticated());
		subject.logout();
	}
}
